mainApp.factory("empFactory",function(){
	
	var service={};

	service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 6
        }
		var list = "";
        RequestUtils.postJson('../employee/getByStatus/1',obj).done(function (resp) {
        	console.log(resp);
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });

	    return list;
	}

	service.getAllByEmpIdOrName=function(employee_id,name){
		console.log(employee_id);
		console.log(name);
		var list = "";
		$.ajax({
	        url: '../employee/get/' + employee_id + '/' + name,
	        type: 'GET',
	        async: 'true',
	        cache: 'no-cache',
	        dataType: 'json',
	        async: false,//同步
	        success: function (result) {
	        	list=result.data;
	        }
	    })
	    console.log(list);
	    return list;
	}
	service.getEmpById=function(id){
		var list = "";
		$.ajax({

	        url: '../employee/getById/' + id,

	        type: 'GET',
	        async: 'true',
	        cache: 'no-cache',
	        dataType: 'json',
	        async: false,//同步
	        success: function (result) {
	        	list=result.data;
	        	
	        }
			
	    })
	    return list;
	}

	
	service.addEmp=function(empObj){
			empObj.information=empObj.jobnum+'.jpg';
			empObj.pwd='123';
			empObj.status='1';
		var names = [];
		console.log(empObj);
		names.push({name:empObj.information })
		 RequestUtils.fileUpload('../employee/loadphoto',['newPhotoFile'], names);
		RequestUtils.postForm('../employee/add',empObj).done(function (resp) {
			if(resp.resultCode==0){
                CommonUtils.buildFailHint("新增数据","成功");
				location.reload(true);
			}else{
                CommonUtils.buildFailHint('新增数据', '失败');
			}
		});
	}
	
	
	service.UpdateEmp=function(emp){
		console.log(emp.number);
		var Obj = {
		 'number': emp.number,
		 'jobnum': emp.jobnum,
		 'name': emp.name,
		 'departId': emp.departId,
		 'post': emp.post,
		 'phone': emp.phone,
		 'd_name': emp.d_name,
		 'd_phone': emp.d_phone,
		 'd_email': emp.d_email,
		 'status': emp.status,
		 'pwd': emp.pwd,
		 'information':emp.information
		 };
	    RequestUtils.postForm('../employee/update', Obj)
        .done(function (resp) {
        	if(resp.resultCode==0){
        		alert("修改成功");
				location.reload(true);
			}else{
        		alert("失败")
        	}
        	
        });
	};

	//查询所有service
	service.selectAll=function(){
		var list="";
        RequestUtils.getJson('../employee/getAll').done(function (resp) {
        	console.log(resp);
            if(resp.resultCode==0){
            	list=resp.data;
                CommonUtils.buildFailHint("查询数据","成功");
            }else{
                CommonUtils.buildFailHint('查询数据', '失败');
            }
        });
        return list;
	};

	    return service;
})

mainApp.controller("empController",function($scope,$http,empFactory,departFactory){
	$scope.title="在职员工"
	$scope.list="";
	$scope.dateTime=CommonUtils.getDateTime();
	console.log($scope.dateTime);



	//增加模态框里的数据绑定
	$scope.addEmpData;

	
	//获取数据 全部的
    $scope.list=empFactory.getListData(1);
	$scope.emplist=$scope.list.items;
	console.log($scope.emplist);
	
	//查询数据 姓名或者工号
    $scope.empWhere;
    $scope.selectEmpByWhere=function(empWhere){
		if($("#selectEmpJobnum").val()==''||$("#selectEmpJobnum").val()==null){
            empWhere.jobnum='null';
		}
		if($("#selectEmpName").val()==''||$("#selectEmpName").val()==null){
            empWhere.name='null';
		}
		$scope.emplist=empFactory.getAllByEmpIdOrName(empWhere.jobnum,empWhere.name);
		console.log($scope.emplist);
        empWhere.jobnum="";
        empWhere.name="";
	}

    //在部门处处查询的所有部门信息
    var departDatas=departFactory.selectAll();
    $scope.departList=departDatas;

	//增加
	$scope.addEmp=function(emp,departObj){
		var empObj = {
			      'number': emp.number,
			      'jobnum': emp.jobnum,
			      'name': emp.name,
			      'departId': departObj.id,
			      'post': emp.post,
			      'phone': emp.phone,
			      'd_name': departObj.manager_name,
			      'd_phone': departObj.phone,
			      'd_email': departObj.email,
			      'status': emp.status,
			      'information':emp.information
		};
		console.log(empObj);
		//增加
		empFactory.addEmp(empObj);

		$("#addEmpModal").modal("hide");
		$scope.$apply();//需要手动刷新
	}
	

	$scope.Emp;
	//查询单人
	$scope.selectEmp=function(id){
		console.log(id);
        var emp=empFactory.getEmpById(id);
        $scope.Emp=emp;
        $scope.departList={
            'departId': emp.departId,
            'manager_name': emp.d_name,
            'phone': emp.d_phone,
            'email': emp.d_email
		};
        console.log($scope.Emp);
		$("#EmpModal").modal("show");

	}

	//更新按钮
	$scope.Update=function(emp,departObj){
        var empObj = {
        	"id":emp.id,
            'number': emp.number,
            'jobnum': emp.jobnum,
            'name': emp.name,
            'departId': departObj.id,
            'post': emp.post,
            'phone': emp.phone,
            'd_name': departObj.manager_name,
            'd_phone': departObj.phone,
            'd_email': departObj.email,
            'status': emp.status,
            'information':emp.information
        };
		console.log(empObj);
		//更新
		empFactory.UpdateEmp(empObj);
		$("#EmpModal").modal("hide");
		//重新刷新页面
		$scope.$apply();
	}
	
	$scope.dimission=function(){
		if(window.confirm("确定这位员工离职？")){
			console.log("离职")
	    }
		
	}

	$scope.openAddEmpModal=function(){

		$("#addEmpModal").modal("show");
	}

	$scope.openSelectEmpModal=function(){

		$("#selectEmpModal").modal("show");
	}

    //翻页
    $scope.NextListData=function(pageIndex){
		//分页后的数据
        $scope.list=empFactory.getListData(pageIndex);
        //每一页的数据
        $scope.emplist=$scope.list.items;

    }

	CommonUtils.setDatetimepicker("datetimepicker");
	CommonUtils.setDatetimepicker("updateDatetimepicker");
})